package com.tingyuge.base.jdk.io.nio.filechannel;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author xiaodao
 * @version 1.0
 * @description write demo
 */
public class FileChannelDemo2 {
    public static void main(String[] args) throws Exception {
        String path = FileChannelDemo2.class.getResource("/").getPath();
        String resourcePath = path.replace("target/classes/", "src/main/resources");
        RandomAccessFile accessFile = new RandomAccessFile(resourcePath+"/FileChannelDemo2.txt","rw");
        FileChannel channel = accessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        String str = "hello write";
        buffer.put(str.getBytes());

        buffer.flip();

        while (buffer.hasRemaining()){
            channel.write(buffer);
        }
        channel.close();
    }
}
